//
//  SampuxModelSegView.m
//  Sampux
//
//  Created by rzm on 16/7/8.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxModelSegView.h"

@implementation SampuxModelSegView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _segBtnArray = [NSMutableArray array];
        [self setupSubView];
    }
    return self;
    
}

- (void)setupSubView
{
//    _modeView = [[UIView alloc]initWithFrame:CGRectMake(self.width/2 - Sampux6WScale(80)/2, 10, Sampux6WScale(80), self.height - 20)];
        _modeView = [[UIView alloc]initWithFrame:CGRectMake(self.width/2 - self.width * 0.6/2, self.height * 0.1, self.width * 0.6, self.height * 0.8)];
    [self addSubview:_modeView];
    _modeView.backgroundColor = THSystemClearColor;
    [_modeView setCorner:_modeView.height/2];
    self.modeView = _modeView;
    _modeView.layer.borderWidth = 1;
    _modManuallyLab.layer.borderColor = THWhiteAlplaColor(0.2).CGColor;
    
    _btnBackimgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _modeView.height, _modeView.height)];
    [_modeView addSubview:_btnBackimgView];
    _btnBackimgView.backgroundColor = THDeFaultBlueColor;
    _btnBackimgView.userInteractionEnabled = YES;
    [_btnBackimgView setCorner:_btnBackimgView.height/2];

    _segOneBtn = [UIButton createButton];
    _segOneBtn.frame = CGRectMake(0, 0, _modeView.width/2, _modeView.height);
    [_modeView addSubview:_segOneBtn];
    _segOneBtn.backgroundColor = THSystemClearColor;
    _segOneBtn.tag = SampuxModelSegView_segLeft;
    [_segOneBtn addTarget:self action:@selector(clickSegmentButtonWithTag:) forControlEvents:UIControlEventTouchUpInside];
    
    _segTwoBtn = [UIButton createButton];
    _segTwoBtn.frame = CGRectMake(_segOneBtn.right, 0, _modeView.width/2, _modeView.height);
    [_modeView addSubview:_segTwoBtn];
    _segTwoBtn.backgroundColor = THSystemClearColor;
    [_segTwoBtn addTarget:self action:@selector(clickSegmentButtonWithTag:) forControlEvents:UIControlEventTouchUpInside];
    _segTwoBtn.tag = SampuxModelSegView_segRight;
    [self.segBtnArray addObject:_segOneBtn];
    [self.segBtnArray addObject:_segTwoBtn];
    
    
    _timeLab = [[UILabel alloc]initWithFrame:CGRectMake(_modeView.left - self.width * 0.3, _modeView.y, self.width * 0.3, _modeView.height)];
    [self addSubview:_timeLab];
    _timeLab.textAlignment = NSTextAlignmentLeft;
    _timeLab.textColor = THWhiteAlplaColor(0.6);

    _modManuallyLab = [[UILabel alloc]initWithFrame:CGRectMake(_modeView.right, _modeView.y, self.width * 0.3, _modeView.height)];
    [self addSubview:_modManuallyLab];
    _modManuallyLab.textAlignment = NSTextAlignmentRight;
    _modManuallyLab.textColor = THWhiteAlplaColor(0.6);
}

- (void)clickSegmentButtonWithTag:(UIButton *)button
{
    NSLog(@"%ld",(long)button.tag);
    for (UIButton *btn in self.segBtnArray) {
        btn.selected = NO ;
    }
    button.selected = YES ;
    if (button.tag == 0) {
        [self moveBackImageViewToLeft];
    }else{
        [self moveBackImageViewToRight];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _modeView.frame = CGRectMake(self.width/2 - self.width * 0.4/2, self.height * 0.1, self.width * 0.4, self.height * 0.8);
    [_modeView setCorner:_modeView.height/2];
    _modeView.layer.borderWidth = 1;
    _modManuallyLab.layer.borderColor = THWhiteAlplaColor(0.2).CGColor;
    _btnBackimgView.frame = CGRectMake(0, 0, _modeView.height, _modeView.height);
    [_btnBackimgView setCorner:_btnBackimgView.height/2];
    _segOneBtn.frame = CGRectMake(0, 0, _modeView.width/2, _modeView.height);
    _segTwoBtn.frame = CGRectMake(_segOneBtn.right, 0, _modeView.width/2, _modeView.height);
    _timeLab.frame = CGRectMake(_modeView.left - self.width * 0.3, _modeView.y, self.width * 0.3, _modeView.height);
}

//左移
-(void)moveBackImageViewToLeft
{
    [UIView animateWithDuration:0.3 animations:^{
        self.btnBackimgView.x = 0 ;
    } completion:^(BOOL finished) {
    }];
}
//右移
- (void)moveBackImageViewToRight
{
    [UIView animateWithDuration:0.3 animations:^{
        self.btnBackimgView.x = self.btnBackimgView.width ;
    } completion:^(BOOL finished) {
    }];
}

@end
